<template>
  <div id="login-container" style="border-radius: 30px">
    <div class="login-form" style="border-radius: 100px;border: 5px solid  coral;margin-top: 50px">
      <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="80px" class="demo-ruleForm" style="margin-top: 50px">
        <el-form-item label="Username" prop="username" style="width: 420px;background-color: transparent;opacity:0.5">
          <el-input v-model="loginForm.username" placeholder="Enter your username"/>
        </el-form-item>
        <el-form-item label="Password" prop="password" style="width: 420px;opacity:0.5">
          <el-input class="el-input" v-model="loginForm.password" type="password" placeholder="Enter your password"/>
        </el-form-item>

        <div style="display: flex;">
          <el-form-item>
            <el-button style="width: 200px;border-radius: 30px" type="primary" @click="login" size="medium">login</el-button>
            <!--          <el-link type="warning" to="/Register">没有账号去注册</el-link>-->
          </el-form-item>
          <el-form-item>
            <router-link to="/Register" class="btn btn-primary"  style="color: coral">没有账号去注册</router-link>
          </el-form-item>
        </div>
        <el-form-item>
          <router-link to="/ResetPassword" class="btn btn-primary"  style="color: coral">忘记密码</router-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{required: true, message: 'Please enter username', trigger: 'blur'}],
        password: [{required: true, message: 'Please enter password', trigger: 'blur'}]
      }
    };
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          axios.post('http://localhost:8082/user/login', this.loginForm)
              .then(resp => {
                if (resp.data.code === 200) {
                  localStorage.setItem('access-admin', JSON.stringify(resp.data.data));

                  this.$notify.success({
                    title :"成功",
                    type :"success",
                    message :"登录成功",

                  })
                  if (resp.data.data.userRole === "ROLE_USER") {
                    this.$router.replace({path: '/'});
                  } else if (resp.data.data.userRole === "ROLE_ADMIN") {
                    this.$router.replace({path: '/manager'});
                  }
                } if(resp.data.code===500) {
                  this.$notify.error({
                    title: '错误',
                    message: resp.data.msg,
                  });

                }
              })
              .catch(err => {
                alert(err);
              });
        }
      });
    }
  }
}
</script>

<style>
#login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(to bottom, #6394f8, #62d8aa);
  background-image: url('@/assets/login.jpg');
  background-size: cover;
  background-position: center;
}

.login-form {
  width: 500px;
  height: 300px;
  border-radius: 10px;
  border: 3px solid skyblue;
  margin: 0 auto;
}

.demo-ruleForm {
  margin-top: 20px;
}


</style>